#/usr/bin/python27
#-*- coding: utf-8 -*-
#  Copyright 2011-2013 wuweihui@cn.ibm.com
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import ConfigParser
import os
   
class CimConfig(ConfigParser.RawConfigParser):
   def __init__(self, *args, **kws):
      ConfigParser.RawConfigParser.__init__(self, *args, **kws)
   
   def read(self, filenames=None):
      if not filenames:
         if os.path.exists('config/config.cfg'):
            ConfigParser.RawConfigParser.read(self, 'config/config.cfg')
         else:
            ConfigParser.RawConfigParser.read(self, os.path.join(os.path.split(__file__)[0], 'config.cfg'))
      else:
         ConfigParser.RawConfigParser.read(self, filenames)
   
   def reload_config(self):
      self.__init__()
      self.read()
         
   def save_config(self, cf):
      with open('config/config.cfg', 'wb') as fp:
         if isinstance(cf, basestring):
            fp.write(cf)
         else:
            cf.write(fp)
      self.reload_config()
   
config = CimConfig()
config.read()

   
if __name__ == '__main__':
   import StringIO
   buf = StringIO.StringIO()
   config.write(buf)
   print buf.getvalue()

